<?php

/*
Info: Handles the real time notification updates
Author: Pritam Pebam
Version: 1.0
Contact: ptamzz@gmail.com; @ptamzz
URL: http://12bubbles.com
*/

	session_start();
	require_once('../inc/fnc.php');
	$flag = 0;
	$markUp = '';
	$notinum = 0;

	$query = "SELECT * FROM `notification` WHERE `followerid` = {$_SESSION['userId']} AND delflag = 'N' ORDER BY crtdtime DESC LIMIT 5";
	$result = mysql_query($query, $connection);	

	while($row = mysql_fetch_array($result)){
		$flag = 1;
		$readflag = '';
		$readflag = $row['readflag'];
		if($row['event']== 1) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']}";
			$results = mysql_query($querys,$connection);
			if(!$result) { echo "error"; }
			
			while($rows = mysql_fetch_array($results)) {
				$sttitle = $rows['stline'];
				$entryl = substr($rows['entryl'], 0, 40);
				if(strlen($rows['entryl']) > 40) { $entryl .= '...'; }
			}
			$querys = mysql_query("SELECT count(entrylineid) as cnt from entrylinevote where entrylineid = {$row['uniqueentryid']} and UNIX_TIMESTAMP(crtdtime) <= {$row['crtdtime']} and delflag = 'N' and vote = 1",$connection);
			while($rows = mysql_fetch_array($querys)) { $votecnt = $rows['cnt']; }
			$cnttyp = ($votecnt > 1) ? 'likes' : 'like';
			$btmline = "The entry line has received " .$votecnt . " " . $cnttyp;
		}
		
		
		if($row['event']== -1) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']}";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) {
				$sttitle = $rows['stline'];
				$entryl = substr($rows['entryl'], 0, 40);
				if(strlen($rows['entryl']) > 40) { $entryl .= '...'; }
			}
			$querys = mysql_query("SELECT count(entrylineid) as cnt from entrylinevote where entrylineid = {$row['uniqueentryid']} and UNIX_TIMESTAMP(crtdtime) <= {$row['crtdtime']} and delflag = 'N' and vote = -1");
			while($rows = mysql_fetch_array($querys)) { $votecnt = $rows['cnt']; }
			$cnttyp = ($votecnt > 1) ? 'dislikes' : 'dislike';
			$btmline = "The entry line has received " .$votecnt . " " . $cnttyp;
		}
		
		if($row['event']== 2) {
			$querys = "SELECT story.storyname as stline, storyline.storyline as entryl from storyline join story on storyline.storyid = story.storyid where storyline.storylineid = 1 and story.storyid = {$row['storyid']} and storyline.delflag = 'N'";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) {
				$sttitle = $rows['stline'];
				$entryl = substr($rows['entryl'], 0, 40);
				if(strlen($rows['entryl']) > 40) { $entryl .= '...'; }
			}
			$btmline = "A new story has been created.";
		}
		
		if( $row['event']== 3) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']} and entryline.delflag = 'N'";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) {
				$sttitle = $rows['stline'];
				$entryl = substr($rows['entryl'], 0, 40);
				if(strlen($rows['entryl']) > 40) { $entryl .= '...'; }
			}
			
			$btmline = "A new twist has been added.";
		}
		
		if($row['event']== 4) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']} and entryline.delflag = 'N'";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) {
				$sttitle = $rows['stline'];
				$entryl = substr($rows['entryl'], 0, 40);
				if(strlen($rows['entryl']) > 40) { $entryl .= '...'; }
			}
			$btmline = "Congrats! Your twist has been voted up as the next story line.";
		}
		if($row['event']== 5) {
			$querys = "SELECT story.storyname as stline, entryline.entryline as entryl from entryline join story on entryline.storyid = story.storyid where entryline.displayseq = {$row['uniqueentryid']} and story.storyid = {$row['storyid']} and entryline.delflag = 'N'";
			$results = mysql_query($querys);
			while($rows = mysql_fetch_array($results)) {
				$sttitle = $rows['stline'];
				$entryl = substr($rows['entryl'], 0, 40);
				if(strlen($rows['entryl']) > 40) { $entryl .= '...'; }
			}
			$btmline = "The twist has been voted up as the next story line.";
		}
		$class = '';
		if($readflag == 'N'){ $class = 'new'; }
		$markUp .=	"<li class='eachNotice {$class}'>
						<a href='index.php?page=story&id={$row['storyid']}&title=".getStoryTitle($row['storyid'])."&nid={$row['vote']}'>
							<span class='not-msg-icon'></span>
							<div class='left' style='width: 275px;'>
								<h1>{$sttitle}</h1>
								<p>{$entryl}</p>
								<div class='noticeMta'>{$btmline}</div>
							</div>
							<div class='clear'></div>
						</a>
					</li>";
					
					
	}
	$markUp .= '</ul>';
	if ($flag != 1){
		echo '';
	} else  {
		echo $markUp;
	}

?>